#ifdef SENSORS_ENABLE
//logMessage(LOGLEVEL_DEBUG, "SENSORS", "", "");
unsigned long SENSORSPreviousMillis;

/*************************************************************
 *
 * globals
 *
 *************************************************************/
 
void setupSensors() {
}

void readSesnorsDelay() {
  unsigned long tempCurrentMillis = millis();
  if((tempCurrentMillis - SENSORSPreviousMillis) > SENSORS_DELAY || SENSORSPreviousMillis == 0) {          
    SENSORSPreviousMillis = tempCurrentMillis;  
    _readSesnors(); 
  }
}
// ist-zustand (anderen namen fur finden)
int getStatusOfSystem2(int aPin) {
  return (analogRead(aPin))>250?1:0;
}

// soll-zustand (anderen namen fur finden)
int getStatusOfSystem(int aStartTime, int aDuration) {
  int tempSStatus = 0;
  int tempStopTime = (aStartTime + aDuration);
  int tempTimeHour = TIMEhour;
  if (tempTimeHour>=aStartTime) {
    if(tempTimeHour<tempStopTime) {
      return 1;
    }
    return 0;
  }
  int tempNewTime = tempTimeHour + 24;
  if (tempNewTime < tempStopTime) {
    return 1;
  }
  return 0;
}

/*************************************************************
 *
 * intern
 *
 *************************************************************/
 
void _processSensors(int aSystem, int aTimerLightOn, int aTimerLightOffset, int aSensorPin) {
  int tempSStatus = getStatusOfSystem(aTimerLightOn, aTimerLightOffset);
  int lichtSchrank = getStatusOfSystem2(aSensorPin);
  //logMessage(LOGLEVEL_DEBUG, "SENSORS", "analogRead ", lichtSchrank);
  // Licht sollte an sein, ist aber aus
  if (tempSStatus == 1 && lichtSchrank == 0) {
    _alarmNotifier(SENSORS_LIGHTOFF, aSystem);
  }
  // Licht sollte aus sein, ist aber an
  if (tempSStatus == 0 && lichtSchrank == 1) {
    _alarmNotifier(SENSORS_LIGHTON, aSystem);
  } 
  //logMessage(LOGLEVEL_DEBUG, "SENSORS", "tempSStatus: ", tempSStatus); 
}

void _readSesnors() {
  
  // Schrank
  _processSensors(SYSTEM1, getSystemLightOn(SYSTEM1), getTimerDuration(SYSTEM1), SENSORS_LIGHT1_PIN);
  
  // Schrank 2:
  _processSensors(SYSTEM2, getSystemLightOn(SYSTEM2), getTimerDuration(SYSTEM2), SENSORS_LIGHT2_PIN);
}

void _alarmNotifier(int aStatus, int aSystem) {
  logMessage(LOGLEVEL_DEBUG, "SENSORS", "ALARM", "");
  String tempStatus;
  if (aStatus == SENSORS_LIGHTOFF) {
    tempStatus = "SENSORS_LIGHTOFF";  
  }
  if (aStatus == SENSORS_LIGHTON) {
    tempStatus = "SENSORS_LIGHTON";  
  }  
  logMessage(LOGLEVEL_DEBUG, "SENSORS", "status: ", tempStatus);
  logMessage(LOGLEVEL_DEBUG, "SENSORS", "system: ", aSystem);
  #ifdef WEB_ENABLE
  notifyToWeb(aSystem, aStatus);
  #endif
}

#endif
